<template>
  <view>
    <!-- 搜索框 -->
    <my-search @click="gotoSearch"></my-search>
    <!-- 分类选择 -->
    <view class="cate-container">
      <!-- 左侧一级分类 -->
      <scroll-view class="cate-left" scroll-y :style="{height: wh+'px'}">
        <!-- 循环 cateList -->
        <!-- 用循环时候的 i 和 active 进行比较，一样的话就使用 active class -->
        <!-- 绑定点击事件，进行高亮 -->
        <view v-for="(item, i) in cateList" :key="item.cat_id" :class="['cate-left-item', i === active ? 'active' : '']"
          @click="changeActive(i)">
          {{item.cat_name}}
        </view>
      </scroll-view>
      <!-- 右侧二级分类 -->
      <!-- #2 和 scroll-top 属性绑定 -->
      <scroll-view class="cate-right" scroll-y :style="{height: wh+'px'}" :scroll-top="scrollTop">
        <!-- 循环 cateLevel2 二级分类 -->
        <view class="cate-right-item" v-for="(item, index) in cateLevel2" :key="index">
          <!-- 二级分类 -->
          <view class="cate-right-item-title">/{{item.cat_name}}/</view>
          <!-- 三级分类 -->
          <view class="cate-right-item-content">
            <view class="cate-right-item-content-box" v-for="(item2, i2) in item.children" :key="i2"
              @click="gotoGoodsList(item2)">
              <image mode="widthFix" :src="item2.cat_icon"></image>
              <text>{{item2.cat_name}}</text>
            </view>
          </view>
        </view>
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import setBadge from '../../mixins/tabbar-badge.js'
  export default {
    data() {
      return {
        wh: 0,
        active: 0,
        cateList: [],
        cateLevel2: [],
        // #1
        scrollTop: 0
      };
    },
    mixins: [setBadge],
    onLoad() {
      // 获取系统信息
      const sysInfo = uni.getSystemInfoSync()
      // windowHeight 表示窗口的可用高度 = 总高度 - tabBar - navBar
      // 50 表示的是搜索组件的高度 
      this.wh = sysInfo.windowHeight - 50

      this.getCateList()
    },
    methods: {
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/categories')
        if (res.meta.status !== 200) return uni.$showMsg()
        this.cateList = res.message
        // 把一级分类的 children 给初始的二级分类
        this.cateLevel2 = res.message[0].children
      },
      changeActive(i) {
        this.active = i
        // 把当前索引对应的一级分类下的 children（二级分类）给 cateLevel2
        this.cateLevel2 = this.cateList[i].children

        // #3 注意 scroll-view 组件的 scroll-top 值每次不能一样，否则没效果
        // this.scrollTop = this.scrollTop === 0 ? 1 : 0
        this.scrollTop = Math.random()
      },
      gotoGoodsList(item) {
        uni.navigateTo({
          url: '/subpkg/goods_list/goods_list?cid=' + item.cat_id
        })
      },
      gotoSearch(e) {
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    }
  }
</script>

<style lang="scss">
  .cate-container {
    display: flex;

    .cate-left {
      width: 240rpx;

      .cate-left-item {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 120rpx;
        background-color: #efefef;
        font-size: 24rpx;

        // 选中样式
        &.active {
          display: flex;
          justify-content: space-between;
          background-color: #ffffff;

          // 左边红线
          &::before {
            content: '';
            width: 6rpx;
            height: 60rpx;
            background-color: #c00000;
          }

          // 右边白线和背景相同
          &::after {
            content: '';
            width: 6rpx;
            height: 60rpx;
            background-color: #fffff;
          }
        }
      }
    }
  }

  .cate-right {
    background-color: white;

    .cate-right-item {
      .cate-right-item-title {
        display: flex;
        justify-content: center;
        font-weight: bold;
        font-size: 24rpx;
        padding: 20rpx 0;
      }

      .cate-right-item-content {
        display: flex;
        flex-wrap: wrap;

        .cate-right-item-content-box {
          display: flex;
          flex-direction: column;
          justify-content: center;
          align-items: center;
          width: 33.33%;
          padding: 20rpx;
          box-sizing: border-box;

          image {
            width: 100%;
          }

          text {
            font-size: 24rpx;
          }
        }
      }
    }
  }
</style>
